const app = getApp()
import prizeApi from '../../api/prizeApi'
import addressApi from '../../api/addressApi'
import prizeSendApi from '../../api/prizeSendApi'
import msgCheck from '../../util/msgCheck'

import imgCheck from '../../util/imgCheck'
import uploadImgList from '../../util/uploadImgList'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        prizeId: '',
        prize: '',
        address: '',
        content: '',
        imgList: [],
        fileList: [],
        deleteFiles: [],
        action: 'add',
        prizeSendId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.prizeId = options.prizeId
        this.getPrize(options.prizeId)
        this.getAddress()
        this.getPrizeSend()
    },

    async getPrize(prizeId){
        wx.showLoading({
          mask:true
        })
        let data = await  prizeApi.getPrize(prizeId)
    
        wx.hideLoading( )
            
        this.setData({
          prize : data
        })
      },
      async getAddress(){
        let list = await addressApi.getAddress({
            prizeId: this.data.prizeId
        })
        
        if(list.length > 0){
            this.setData({
                address: list[0]
            })
        }
      },

      async getPrizeSend(){
        let list = await prizeSendApi.getPrizeSend({
            prizeId: this.data.prizeId
        })
        
        if(list.length > 0){
            this.setData({
              content: list[0].content,
              imgList: list[0].imgList,
              fileList: list[0].fileList,
              prizeSendId: list[0]._id,
              action: 'update'
            })
        } else {
          this.setData({
           
            action: 'add'
        })
        }
      },

      previewImage(e){
        var url = e.currentTarget.dataset.url
        wx.previewImage({
          urls: this.data.imgList,
          current: url
        })
      },
    
      contentInput: function(e) {
        var val = e.detail.value;
    
        this.setData({
          content: val
        });
      },
      chooseImage:function(){
        wx.chooseMedia({
          count: 9 - this.data.imgList.length,
          mediaType: ['image'],
          success: (res) => {
            let list = res.tempFiles.map(obj => obj.tempFilePath)
            const tempFilePaths = this.data.imgList.concat(list)
            this.setData({
              imgList: tempFilePaths
            })
          }
        })
      },
    
      delImg(e){
      
        var index = e.currentTarget.dataset.index
        this.data.imgList.splice(index,1)
        var fileIds = this.data.fileList.splice(index,1)
        if(fileIds[0]){
          this.data.deleteFiles.push(fileIds[0])
        }
        this.setData({
          imgList: this.data.imgList
        })
      },

      
  async submit(){
    let pass = await msgCheck(this.data.content)
   if(!pass){
      return
   }

   pass =  await imgCheck(this.data.imgList)
   if(!pass){
    return
 }
     

    if(this.data.imgList.length == 0 && (!this.data.content)){
      wx.showToast({
        title:'没有可提交的内容',
        icon:'none'
      })
      return 
    }


    wx.showLoading({
      mask:true
    })
    var res = await uploadImgList(this.data.imgList, this.data.fileList)
    
    
    var params = {
      prizeId: this.data.prizeId,
      fileList: res.map((f) => f.fileID),
      imgList: res.map((f) => f.tempFileURL),
      content:this.data.content,
    }

    if(this.data.action == 'update') {
        params._id = this.data.prizeSendId
        await prizeSendApi.updatePrizeSend(params)

        await wx.cloud.deleteFile({
          fileList: this.data.deleteFiles
        })

        params.type = 'prizeSend'
      
    } else {
      await prizeSendApi.addPrizeSend(params)

      params.type = 'prizeSend'
      
    }
   
    await wx.cloud.callFunction({
      name: 'sendMsg',
      data: {
          type: 'sendProduct',
          prizeId: this.data.prizeId,
      }
  })
   
    wx.setStorageSync('refreshPrizeSend', 1)
    wx.hideLoading()
    wx.navigateBack()
  },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})